package com.jwdf.uda.ui.adapter;

import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UserAddFollowAction;
import com.jwdf.uda.http.action.UserCancelFollowAction;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.view.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Response;

/**
 * Created by fatsfan on 2016/10/11.
 */
public class QuizRecommandAdapter extends RecyclerView.Adapter<QuizRecommandAdapter.ItemQuizRecommandHolder> implements View.OnClickListener {

    private BaseFragment fragment;
    private List<PbBean.PbUserSimple> list = new ArrayList<>();

    public QuizRecommandAdapter(BaseFragment fragment) {
        this.fragment = fragment;
    }

    public List<PbBean.PbUserSimple> getData() {
        return list;
    }

    public void setData(PbBean.PbResUserGetRandRecommendUserList listPB) {
        List<PbBean.PbUserSimple> list = listPB.getUserListList();
        if (list != null && !list.isEmpty()) {
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public ItemQuizRecommandHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemQuizRecommandHolder(fragment.getActivity().getLayoutInflater(), parent);
    }

    @Override
    public void onBindViewHolder(ItemQuizRecommandHolder holder, int position) {
        final PbBean.PbUserSimple item = list.get(position);
        holder.getTvName().setText(item.getNickname());
        final TextView tvInfo = holder.getTvInfo();
        tvInfo.setVisibility(TextUtils.isEmpty(StringUtils.filterNullAndTrim(item.getIntroduction())) ? View.GONE : View.VISIBLE);
        tvInfo.setText(item.getIntroduction());
        holder.getVDesigner().setVisibility(item.getIsDesigner() ? View.VISIBLE : View.GONE);
        ImageLoader.getInstance().displayImage(item.getAvatar().getUrl(), holder.getIcon());

        final PbBean.PbSignUserFollowType followType = item.getFollowType();
        final View vSel = holder.getvSel();
        if (followType == PbBean.PbSignUserFollowType.USER_FOLLOW_TYPE_FANS)
            vSel.setSelected(true);
        else
            vSel.setSelected(false);

        vSel.setTag(position);
        vSel.setOnClickListener(this);

        final View vContent = holder.getvContent();
        vContent.setTag(position);
        vContent.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(final View v) {
        PbBean.PbUserSimple item = list.get((Integer) v.getTag());

        switch (v.getId()) {

            case R.id.vContent:
                UserInfoActivity.start(v.getContext(), item.getUserId(), item.getIsDesigner());
                break;

            case R.id.vSel:
                fragment.showProgressDialog();

                final FragmentActivity aty = fragment.getActivity();

                if (v.isSelected())
                    fragment.onNewRequestCall(UserCancelFollowAction.newInstance(aty, item.getUserId()).request(new AHttpService.IResCallback<PbBean.PbResUserDelFollow>() {
                        @Override
                        public void onCallback(int resultCode, Response<PbBean.PbResUserDelFollow> response, PbBean.PbBaseServerInfo baseServerInfo) {
                            if (fragment.getView() != null) {
                                fragment.closeProgressDialog();
                                if (HttpHelper.showError(fragment.getContext(), resultCode, baseServerInfo, fragment.getString(R.string.error_net))) {
                                    return;
                                }
                            }

                            v.setSelected(false);

                            ConfigUtils.reduceFollowCount(aty);
                        }
                    }));

                else
                    fragment.onNewRequestCall(UserAddFollowAction.newInstance(aty, item.getUserId()).request(new AHttpService.IResCallback<PbBean.PbResUserAddFollow>() {
                        @Override
                        public void onCallback(int resultCode, Response<PbBean.PbResUserAddFollow> response, PbBean.PbBaseServerInfo baseServerInfo) {
                            if (fragment.getView() != null) {
                                fragment.closeProgressDialog();
                                if (HttpHelper.showError(fragment.getContext(), resultCode, baseServerInfo, fragment.getString(R.string.error_net))) {
                                    return;
                                }
                            }

                            v.setSelected(true);
                            ConfigUtils.addFollowCount(aty);
                        }
                    }));

                break;
        }
    }

    public static class ItemQuizRecommandHolder extends RecyclerView.ViewHolder {
        private View vContent;
        private ImageView vSel;
        private CircleImageView icon;
        private ImageView vDesigner;
        private TextView tvName, tvInfo;

        public ItemQuizRecommandHolder(LayoutInflater inflater, ViewGroup parent) {
            this(inflater.inflate(R.layout.item_concern_recommand, parent, false));
        }

        public ItemQuizRecommandHolder(View view) {
            super(view);
            vContent = view;
            tvName = (TextView) view.findViewById(R.id.tvName);
            tvInfo = (TextView) view.findViewById(R.id.tvInfo);
            icon = (CircleImageView) view.findViewById(R.id.icon);
            vDesigner = (ImageView) view.findViewById(R.id.vIsDesigner);
            vSel = (ImageView) view.findViewById(R.id.vSel);
        }

        public View getvContent() {
            return vContent;
        }

        public ImageView getvSel() {
            return vSel;
        }

        public TextView getTvInfo() {
            return tvInfo;
        }

        public CircleImageView getIcon() {
            return icon;
        }

        public ImageView getVDesigner() {
            return vDesigner;
        }

        public TextView getTvName() {
            return tvName;
        }
    }
}
